<?php
date_default_timezone_set('Asia/Shanghai');//时区配置
$conn=mysql_connect("localhost","root","");
mysql_select_db("1touz", $conn);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);

function db_fetch_arrays($sql, $conn) 	{
	$result = mysql_query($sql, $conn);
	$i = 0;
	$_res = array();
	while(@$res = mysql_fetch_array($result,MYSQL_ASSOC)) {
		foreach ($res as $key => $value){
			$_res[$i][$key] = $value;//直接转义
		}
		$i++;
	}
	return $_res;
}

function db_fetch_array($sql,$conn) {
		$result = mysql_query($sql, $conn);
		$res = mysql_fetch_array($result,MYSQL_ASSOC);
		$_res = "";
		if (is_array($res)){
			foreach ($res as $key => $value){
				$_res[$key] =$value;
			}
		}
		$query = $_res;
		return $query;
}


/**
 * 发送短信
 */
function send($mobile,$content)
{
	$http = 'http://www.gysoft.cn/smspost_utf8/send.aspx';
	$data = array
		(
		'username'=>'gsd',			//用户账号
		'password'=>'gsdai.sms',	        //密码
		'mobile'=>$mobile,			//号码
		'content'=>$content      	//内容
		);

	$re=postSMS($http,$data);			//POST方式提交
	if( substr($re,0,2) == 'OK' )  //返回结果为OK1表示成功1条，OK2 表示成功二条，以此类推
	{
		return "1";
	}
	else
	{
		return "0";   //返回错误的详细提示
	}


}

function postSMS($url,$data='')
{
	$row = parse_url($url);
	$host = $row['host'];
	$port = isset($row['port']) && $row['port'] ? $row['port']:80;
	$file = $row['path'];
	$post = '';
	while (list($k,$v) = each($data))
	{
		$post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
	}
	$post = substr( $post , 0 , -1 );
	$len = strlen($post);
	$fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
	if (!$fp) {
		return "$errstr ($errno)\n";
	} else {
		$receive = '';
		$out = "POST $file HTTP/1.1\r\n";
		$out .= "Host: $host\r\n";
		$out .= "Content-type: application/x-www-form-urlencoded\r\n";
		$out .= "Connection: Close\r\n";
		$out .= "Content-Length: $len\r\n\r\n";
		$out .= $post;
		fwrite($fp, $out);
		while (!feof($fp)) {
			$receive .= fgets($fp, 128);
		}
		fclose($fp);
		$receive = explode("\r\n\r\n",$receive);
		unset($receive[0]);
		return implode("",$receive);
	}
}